package com.jfl.utils;

import java.io.File;
import java.io.IOException;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.web.multipart.MultipartFile;

import com.jfl.exception.FileNameLengthLimitExceedException;
import com.jfl.exception.FileSizeLimitExceededException;

/**
 * 文件上传工具类
 *  
 * @author  赵炎
 * @version  [V1.00, 2019年3月14日]
 * @see  [相关类/方法]
 * @since V1.00
 */
public class FileUploadUtil
{
    /**
     * 默认大小 50M
     */
    public static final long DEFAULT_MAX_SIZE = 50 * 1024 * 1024;
    
    /**
     * 默认文件名最大长度 100
     */
    public static final int DEFAULT_FILE_NAME_LENGTH = 100;
    
    public static final String default_ = "";
    
    public static String IMAGE_JPG_EXTENSION = ".jpg";
    
    /** 
     * 上传文件
     * @param file
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static final String upload(MultipartFile file) 
    {
        return null;
    }
    
    /** 
     * 上传文件
     * @param baseDir 目录
     * @param file  文件
     * @param extension 拓展名
     * @return
     * @throws IOException 
     * @see [类、类#方法、类#成员]
     */
    public static final String upload(String baseDir, MultipartFile file, String extension) throws IOException 
    {
        int fileNameLength = file.getOriginalFilename().length();
        if (fileNameLength > DEFAULT_FILE_NAME_LENGTH)
        {
            throw new FileNameLengthLimitExceedException();
        }
        // 存储本地文件名
        String fileName = extractFileName(file, extension);
        File dest = getAbsoluteFile(baseDir, baseDir + fileName);
        file.transferTo(dest);
        
        return fileName;
    }
    
    public static final String upload(String baseDir, MultipartFile file) throws IOException 
    {
        try
        {
            return upload(baseDir, file, FileUploadUtil.IMAGE_JPG_EXTENSION);
        }
        catch (IOException e)
        {
            throw new IOException(e.getMessage(), e);
        }
    }
    
    private static File getAbsoluteFile(String baseDir, String fileName) throws IOException
    {
        File dest = new File(File.separator + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        if (!dest.exists()) {
            dest.createNewFile();
        }
        return dest;
    }

    /** 
     * <一句话功能简述>
     * @param file
     * @param extension
     * @return
     * @see [类、类#方法、类#成员]
     */
    private static String extractFileName(MultipartFile file, String extension)
    {
        String fileName = file.getOriginalFilename();
        fileName = DateUtil.format(DateUtil.getCurrentDate(), "yyyy-MM-dd") + encodeFileName(fileName) + extension;
        return fileName;
    }

    private static String encodeFileName(String fileName)
    {
        fileName = fileName.replace("_", " ");
        return  Md5Hash.toString((fileName + System.nanoTime()).getBytes());
    }

    /** 
     * 文件大小限制
     * @param file
     * @see [类、类#方法、类#成员]
     */
    public void assertAllowed(MultipartFile file) 
    {
        long size = file.getSize();
        if (DEFAULT_MAX_SIZE != -1 && size > DEFAULT_MAX_SIZE)
        {
            throw new FileSizeLimitExceededException();
        }
        
    }
}
